import 'package:flutter/material.dart';
import 'package:untitled/pages/bean/bean_video.dart';
import 'package:untitled/pages/otherVeiw/find_video_page.dart';

class DouPage extends StatefulWidget {
  const DouPage({Key? key}) : super(key: key);

  @override
  _DouPageState createState() => _DouPageState();
}

class _DouPageState extends State with SingleTickerProviderStateMixin {

  List<String> tabTextList = ["关注","推荐"];
  List<Tab> tabWidgetList = [];

  TabController? tabController;
   ///推荐模拟数据
  List <VideoModel> videoList =[];
  ///关注模拟数据
  List <VideoModel> videoList2 =[];

  @override
  void initState() {
    
    super.initState();

    for(var value in tabTextList){
      tabWidgetList.add(
        Tab(text: "$value",)
      );
    }
    tabController = new TabController(length: tabTextList.length, vsync: this);


    // 创建模拟数据
    for(int i =0;i<10;i++){
      VideoModel videoModel = new VideoModel();
      videoModel.videoName = "推荐测试数据$i";
      videoModel.pariseCount = i * 22;
      if(i % 3 == 0){
        videoModel.isAttention = true;
        videoModel.isLike = true;
      } else {
        videoModel.isAttention = false;
        videoModel.isLike = false;
      }
      videoModel.videoImage = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.kts.g.mi.com%2Fhussan_talos65711a5e2fb865dce6af27b9fdcd68f4_1592700800.jpg&refer=http%3A%2F%2Fpic.kts.g.mi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656155080&t=368ff8d370b3dd03c4c3c25605142857";
      videoModel.videoUrl = "https://vd4.bdstatic.com/mda-ja7m2y7r0wbdrk1k/sc/mda-ja7m2y7r0wbdrk1k.mp4?v_from_s=hkapp-haokan-hnb&auth_key=1653564998-0-0-cd4a81497cd845f92f81452c31b239aa&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=0398590616&vid=6382754111816049522&abtest=101830_2-17451_1-3000232_1-3000233_2&klogid=0398590616";

      videoList.add(videoModel);
    }
    for(int i =0;i<3;i++){
      VideoModel videoModel = new VideoModel();
      videoModel.videoName = "关注测试数据$i";
      videoModel.pariseCount = i * 22;
      if(i % 3 == 0){
        videoModel.isAttention = true;
        videoModel.isLike = true;
      } else {
        videoModel.isAttention = false;
        videoModel.isLike = false;
      }
      videoModel.videoImage = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.kts.g.mi.com%2Fhussan_talos65711a5e2fb865dce6af27b9fdcd68f4_1592700800.jpg&refer=http%3A%2F%2Fpic.kts.g.mi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656155080&t=368ff8d370b3dd03c4c3c25605142857";
      videoModel.videoUrl = "https://vd4.bdstatic.com/mda-ja7m2y7r0wbdrk1k/sc/mda-ja7m2y7r0wbdrk1k.mp4?v_from_s=hkapp-haokan-hnb&auth_key=1653564998-0-0-cd4a81497cd845f92f81452c31b239aa&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=0398590616&vid=6382754111816049522&abtest=101830_2-17451_1-3000232_1-3000233_2&klogid=0398590616";

      videoList2.add(videoModel);
    }
  }
  @override
  Widget build(BuildContext context) {
    return buildRootBody();
    
  }

  Widget buildRootBody(){
    return Scaffold(
      body: Stack(
        children: [
          Positioned(
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            child: Container(
              color: Colors.black,
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            child: buildTableViewWidget(),
          ),
          Positioned(
            left: 0,
            right: 0,
            top: 54,
            bottom: 0,
            child: buildTabBarWidget(),
          ),
          
        ],
      ),
    );
  }

  buildTableViewWidget(){
    return TabBarView(
      controller: tabController,
      children: tabTextList.map<Widget>((value) => buildTableViewItemWidget(value)).toList(),
    );

  }
  buildTabBarWidget(){
    return Container(
      // 对齐在顶部中间
      alignment:Alignment.topCenter,
      child: TabBar(
        controller: tabController,
        // 指示器的颜色
        indicatorColor: Colors.white,
        // 指示器的高度
        indicatorWeight: 2.0,
        isScrollable: true,
        // 指示器的宽度与文字对齐
        indicatorSize: TabBarIndicatorSize.label,
        tabs: tabWidgetList,
      ),
    );
  }

 // 上下滑动页面
  buildTableViewItemWidget(String value){
    List<VideoModel> list = [];
    if(value == "推荐"){
      list = videoList;
    }else{
      list = videoList2;
    }
      return PageView.builder(
        // 子条目的个数
        itemCount: videoList.length,
        // 上下滑动
        scrollDirection: Axis.vertical,
        itemBuilder: (BuildContext context,int index){
        VideoModel videoModel = list[index];
        return buildPageViewItemWidget(value,videoModel);

      });
  }
 
 buildPageViewItemWidget(String value,VideoModel videoModel){
    return FindVideoItemPage(value, videoModel);
 }

}


 

